package com.dark;

import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

import java.util.Iterator;
import java.util.Set;

import static com.google.common.base.Preconditions.checkArgument;

/**
 * Created by tengxue on 16-6-14.
 */
public class MultisetDemo {
    public static void main(String[] args){
        Bug one=new Bug();
        one.setUuid("123");
        one.setAccount("a");
        one.setAddress("a");

        Bug two=new Bug();
        two.setUuid("234");
        two.setAccount("b");
        two.setAddress("b");

        Bug three=new Bug();
        three.setUuid("456");
        three.setAccount("c");
        three.setAddress("c");

        Bug four=new Bug();
        four.setUuid("123");
        four.setAccount("a");
        four.setAddress("a");

        Multiset<Bug> multiset= HashMultiset.create();
        multiset.add(one);
        multiset.add(two);
        multiset.add(three);
        multiset.add(four);
        //统计相同元素的个数
        int sum=multiset.count(one);
        System.out.println(sum);

        //返回的是不重复的元素
        for (Multiset.Entry<Bug> entry:multiset.entrySet()){
            System.out.println("the element is :"+entry.getElement());
            System.out.println("the element's count is :"+entry.getCount());
        }

        System.out.println("-----------------------------------------------------------");
        //返回的是不重复的元素
        Set<Bug> set=multiset.elementSet();
        for (Bug item:set){
            System.out.println("the element is :"+item);
        }

        System.out.println("-----------------------------------------------------------");
        //这里使用迭代能够返回所有的元素
        Iterator<Bug> iterator= multiset.iterator();
        while (iterator.hasNext()){
            System.out.println("the element is :"+iterator.next());
        }

    }

    /**
     * Multiset基本操作.使用的是int类型
     */
    private static void  integerCollectionDemo(){
        Multiset<Integer> multiset= HashMultiset.create();
        multiset.add(1);
        multiset.add(2);
        multiset.add(3);
        multiset.add(4);
        multiset.add(1);

        checkArgument(multiset.size() == 5, "collection's size is wrong!");
        System.out.println(multiset.size());
        System.out.println(multiset.entrySet().size());

        //统计相同元素的个数
        int sum=multiset.count(1);
        System.out.println(sum);

        //返回的是不重复的元素
        for (Multiset.Entry<Integer> entry:multiset.entrySet()){
            System.out.println("the element is :"+entry.getElement());
            System.out.println("the element's count is :"+entry.getCount());
        }

        System.out.println("-----------------------------------------------------------");
        //返回的是不重复的元素
        Set<Integer> set=multiset.elementSet();
        for (Integer item:set){
            System.out.println("the element is :"+item);
        }

        System.out.println("-----------------------------------------------------------");
        //这里使用迭代能够返回所有的元素
        Iterator<Integer> iterator= multiset.iterator();
        while (iterator.hasNext()){
            System.out.println("the element is :"+iterator.next());
        }

        System.out.println("-----------------------------------------------------------");

        //删除的话,这里只删除了其中一个元素.
        multiset.remove(1);
        //这样就能删除相同的元素
        multiset.setCount(1, 0);
        //这里使用迭代能够返回所有的元素
        iterator= multiset.iterator();
        while (iterator.hasNext()){
            System.out.println("the element is :"+iterator.next());
        }

        System.out.println("-----------------------------------------------------------");
        //返回的是不重复的元素
        for (Multiset.Entry<Integer> entry:multiset.entrySet()){
            System.out.println("the element is :"+entry.getElement());
            System.out.println("the element's count is :"+entry.getCount());
        }

    }

    static class Bug{
        private String uuid;
        private String account;
        private String address;

        public String getUuid() {
            return uuid;
        }

        public void setUuid(String uuid) {
            this.uuid = uuid;
        }

        public String getAccount() {
            return account;
        }

        public void setAccount(String account) {
            this.account = account;
        }

        public String getAddress() {
            return address;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            Bug bug = (Bug) o;

            return !(uuid != null ? !uuid.equals(bug.uuid) : bug.uuid != null);

        }

        @Override
        public int hashCode() {
            return uuid != null ? uuid.hashCode() : 0;
        }

        @Override
        public String toString() {
            return "Bug{" +
                    "uuid='" + uuid + '\'' +
                    ", account='" + account + '\'' +
                    ", address='" + address + '\'' +
                    '}';
        }
    }
}
